//
//  HuanKPlanV.m
//  BrushFace
//
//  Created by timer_q on 16/1/17.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "HuanKPlanV.h"
#import "HuiKuanHeaderV.h"
#import "HuiKuanFooterV.h"
#import "HuiKuanCell.h"

@implementation HuanKPlanV

{
    UITableView *_tableView;
    CGFloat viewHeight;
    HuiKuanHeaderV *huiKuanHeader;
    HuiKuanFooterV *huiKuanFoot;
    NSMutableArray *_dataArray;
    UITableView *_baseTable;
    NSDictionary *_myShouYi;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        viewHeight = frame.size.height;
        _dataArray = [[NSMutableArray alloc] init];
        [self UIConfig];
    }
    return self;
}
#pragma mark---xuxuejiao

- (void)UIConfig{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, viewHeight) style:UITableViewStylePlain];
    huiKuanHeader = [HuiKuanHeaderV loadBundleNib];
    huiKuanHeader.frame = CGRectMake(0, 0, SCREEN_WIDTH, 260);
    huiKuanFoot = [HuiKuanFooterV loadBundleNib];
    
    huiKuanFoot.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
    _tableView.tableFooterView = huiKuanFoot;
    _tableView.tableHeaderView = huiKuanHeader;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_tableView];
}

#pragma mark ----------------------
#pragma mark - tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HuiKuanCell *cell = [HuiKuanCell loadBundleNib];
    NSDictionary *subDict = [_dataArray objectAtIndex:indexPath.row];
    cell.moneyLabel.text = [NSString stringWithFormat:@"%@",[subDict objectForKey:@"monthcapital4view"]];
    cell.dateLabel.text = [NSString stringWithFormat:@"%@",[subDict objectForKey:@"paymentdate"]];
    cell.lixiLabel.text = [NSString stringWithFormat:@"%@",[subDict objectForKey:@"monthinterest4view"]];
    
#pragma mark ---yueyunpeng
    
    
    
    huiKuanFoot.yingshouLabel.text = [NSString stringWithFormat:@"应还款总额:%@(含本金¥%@、收益¥%@)",[_myShouYi objectForKey:@"totalRepayamountView"],[_myShouYi objectForKey:@"totalCapitalView"],[_myShouYi objectForKey:@"totalInterestView"]];
    [huiKuanFoot.yingshouLabelY setConstant:-10];
    huiKuanFoot.jiangliLabel.hidden = YES;
    
#pragma mark ------------------
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}
- (void)setHuanKuanData:(id)data{
    huiKuanHeader.fangshiLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还款方式：%@",[data objectForKey:@"repaytype"]] andlocation:5 andFirstColor:[UIColor blackColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.qiciLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还款期次：%@次",[data objectForKey:@"repaycount"]] andlocation:5 andFirstColor:[UIColor blackColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.startLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还款开始日期：%@",[self timerStrWithTime:[data objectForKey:@"startrepaydate"]]] andlocation:7 andFirstColor:[UIColor blackColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.stopLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还款截止日期：%@",[self timerStrWithTime:[data objectForKey:@"endrepaydate"]]] andlocation:7 andFirstColor:[UIColor blackColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.yijinghuikuanLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"已经还款：%@",[data objectForKey:@"realrepayamount"]] andlocation:5 andFirstColor:[UIColor redColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.haixuhuikuanLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"还需还款：%.2f",[[NSString stringWithFormat:@"%@",[data objectForKey:@"dutorepayamount"]] floatValue]] andlocation:5 andFirstColor:[UIColor redColor] andLastColor:[UIColor lightGrayColor]];
    huiKuanHeader.xiaqihuikuanLabel.attributedText = [self labelStr:[NSString stringWithFormat:@"下期还款：%@",[data objectForKey:@"nextrepayamount"]] andlocation:5 andFirstColor:[UIColor redColor] andLastColor:[UIColor lightGrayColor]];
    if (_baseType == 0) {
        huiKuanHeader.yuguhuankuanriLabel.text = @"预计还款日";
    }
    [_dataArray removeAllObjects];
    [_dataArray addObjectsFromArray:[data objectForKey:@"data"]];
    _myShouYi = [[NSDictionary alloc] initWithDictionary:data];
    [_tableView reloadData];
}

- (NSMutableAttributedString *)labelStr:(NSString *)baseStr andlocation:(NSInteger)location andFirstColor:(UIColor *)fc andLastColor:(UIColor *)lc{
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:baseStr];
    [str addAttribute:NSForegroundColorAttributeName value:fc range:NSMakeRange(0, location)];
    [str addAttribute:NSForegroundColorAttributeName value:lc range:NSMakeRange(location, str.length-location)];
    return str;
    
}


- (NSString *)timerStrWithTime:(NSString *)time{
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:[time doubleValue]/1000+28800];
    NSLog(@"date:%@",[detaildate description]);
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}
- (void)baseTableView:(UITableView *)baseTable{
    _baseTable = baseTable;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_tableView.contentOffset.y<0) {
        _baseTable.contentOffset = _tableView.contentOffset;
    }
}
@end
